package summary;

/**
 * @Author: 海琳琦
 * @Date: 2022/3/11 11:20
 * https://leetcode-cn.com/problems/unique-binary-search-trees/
 */
public class Title96 {

    /**
     * dp[i]表示i个节点的二叉搜索树有多少种
     * 递推公式：dp[i] += dp[j - 1] * dp[i - j]  j属于（1..i)相当于头节点的值
     * 初始化：dp[0] = 0 dp[1] = 1
     * @param n
     * @return
     */
    public static int numTrees(int n) {
        int[] dp = new int[n + 1];
        //初始化
        dp[0] = 1;
        dp[1] = 1;
        for (int i = 2; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                dp[i] += dp[j - 1] * dp[i - j];
            }
        }
        return dp[n];
    }

    public static void main(String[] args) {
        int i = numTrees(3);
    }
}
